﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using WebShop.Contracts;

namespace WebShop.DAL.Repositories
{
    public partial class NWDB : DbContext
    {
        public NWDB()
            : base("name=NWDB")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<Role>().HasMany(r => r.Users).WithMany(u => u.Roles).Map(m => m.ToTable("UserRole").MapLeftKey("UserID").MapRightKey("RoleID"));
            modelBuilder.Entity<User>().HasMany(u => u.Roles).WithMany(r => r.Users).Map(m => m.ToTable("UserRole").MapLeftKey("UserID").MapRightKey("RoleID"));
            /* modelBuilder.Entity<Order_Details>()
                     .HasRequired(m => m.Orders)
                     .WithMany(t => t.Order_Details)
                     .HasForeignKey(m => m.OrderID)
                     .WillCascadeOnDelete(false);

             modelBuilder.Entity<Order_Details>()
                         .HasRequired(m => m.Products)
                         .WithMany(t => t.Order_Details)
                         .HasForeignKey(m => m.ProductID)
                         .WillCascadeOnDelete(false);*/
            /* var properties = new[]
         {
             modelBuilder.Entity<Product>().Property(product => product.UnitPrice),
             modelBuilder.Entity<Order>().Property(order => order.Freight),
             modelBuilder.Entity<Order_Detail>().Property(detail => detail.UnitPrice)
         };

             properties.ToList().ForEach(property =>
             {
                 property.Precision = 10;
                 property.Scale = 2;
             });
             */

            //modelBuilder.Entity<Order>().HasMany(u => u.Order_Details).WithRequired(a => a.Order).WillCascadeOnDelete(true);
            //modelBuilder.Entity<Product>().HasMany(u => u.Order_Details).WithRequired(a => a.Product).WillCascadeOnDelete(true);

            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            base.OnModelCreating(modelBuilder);
        }


        public DbSet<Category> Categories { get; set; }
        public DbSet<Order_Detail> Order_Details { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Shipper> Shippers { get; set; }
        public DbSet<Supplier> Suppliers { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<LogItem> Log { get; set; }
        public DbSet<LogEventType> LogEventType { get; set; }
    }
}
